package com.tpyule.tpclass.service.impl;

import com.tpyule.tpclass.dao.QuestionDao;
import com.tpyule.tpclass.entity.bean.FailResultBean;
import com.tpyule.tpclass.entity.bean.ResultBean;
import com.tpyule.tpclass.entity.bean.SubscriptionAction;
import com.tpyule.tpclass.entity.bean.SuccessResultBean;
import com.tpyule.tpclass.entity.bo.MyQuestionBo;
import com.tpyule.tpclass.entity.bo.QuestionAnswerBo;
import com.tpyule.tpclass.entity.bo.QuestionDetailBo;
import com.tpyule.tpclass.entity.dto.MyQuestionQueryDto;
import com.tpyule.tpclass.entity.dto.QuestionAnswerDto;
import com.tpyule.tpclass.entity.dto.QuestionQueryDto;
import com.tpyule.tpclass.entity.pojo.Question;
import com.tpyule.tpclass.entity.pojo.QuestionFollow;
import com.tpyule.tpclass.entity.pojo.Subscription;
import com.tpyule.tpclass.service.QuestionService;
import com.tpyule.tpclass.service.SubscriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author zzz
 * @version 1.0
 * @date 2020/4/15 21:35
 */
@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    QuestionDao questionDao;
    @Autowired
    SubscriptionService subscriptionService;
    @Override
    public ResultBean addQuestion(Question question) {
        ResultBean resultBean = null;
        if (question!=null){
            question.setQuestionId(UUID.randomUUID().toString().toUpperCase());
            int result = questionDao.addQuestion(question);
            Subscription subscription = new Subscription();
            subscription.setSubscriptionId(UUID.randomUUID().toString().toUpperCase());
            subscription.setTargetId(question.getQuestionId());
            subscription.setTargetType("question");
            subscription.setAction(SubscriptionAction.new_answer);
            subscription.setUserId(question.getUserId());
            subscription.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            subscriptionService.addSubscription(subscription);
            if(result!=0){
                resultBean = new SuccessResultBean(question,"添加成功");
            }else {
                resultBean = new FailResultBean("添加失败");
            }
        }else{
            resultBean = new FailResultBean("数据错误");
        }
        return resultBean;
    }

    @Override
    public QuestionDetailBo getQuestionDetail(QuestionQueryDto dto) {
        return questionDao.getQuestionDetail(dto);
    }

    @Override
    public List<QuestionAnswerBo> getListQuestion(QuestionAnswerDto dto) {
        if(dto.getPage()>0&&dto.getPageSize()>0){
            dto.setStart((dto.getPage() - 1) * dto.getPageSize());
        }
        return questionDao.getListQuestion(dto);
    }

    @Override
    public List<MyQuestionBo> getListMyQuestion(MyQuestionQueryDto dto) {
        if(dto.getPage()>0&&dto.getPageSize()>0){
            dto.setStart((dto.getPage() - 1) * dto.getPageSize());
        }
        return questionDao.getListMyQuestion(dto);
    }

    @Override
    public int addFollow(QuestionFollow questionFollow) {
        questionFollow.setId(UUID.randomUUID().toString().toUpperCase());
        Subscription subscription = new Subscription();
        subscription.setSubscriptionId(UUID.randomUUID().toString().toUpperCase());
        subscription.setTargetId(questionFollow.getQuestionId());
        subscription.setTargetType("question");
        subscription.setAction(SubscriptionAction.f_new_answer);
        subscription.setUserId(questionFollow.getUserId());
        subscription.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        subscriptionService.addSubscription(subscription);
        return questionDao.addFollow(questionFollow);
    }

    @Override
    public int updateFollow(String id) {
        return questionDao.updateFollow(id);
    }

    @Override
    public List<MyQuestionBo> getFollowQuestion(MyQuestionQueryDto dto) {
        if(dto.getPage()>0&&dto.getPageSize()>0){
            dto.setStart((dto.getPage() - 1) * dto.getPageSize());
        }
        return questionDao.getFollowQuestion(dto);
    }
}
